import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
import { MailModule } from "../../../modules/MailModule";
import { DataEvent } from "../events/DataEvent";
import { SystemSetter } from "../view/system/SystemSetter";
import { GameProxy } from "../mvc/GameProxy";
import { SMail } from "../../../Message/Game/SMail";
import { GameController } from "../mvc/GameController";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { IMailModule } from "../../../modules/interfaces/IMailModule";
type int = number;
//class MailController
    
    export  class MailController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.MailSend, this.sendMail);
            Dispatcher.addEventListener(EventName.MailRemove, this.removeMail);
            Dispatcher.addEventListener(EventName.MailQuery, this.queryMail);
            Dispatcher.addEventListener(EventName.MailGetAttachment, this.getMailAttachment);
            Dispatcher.addEventListener(EventName.MailRead, this.readMail);
            Dispatcher.addEventListener(EventName.MailQuerySuccess, this.onMailQuerySuccess);
            Dispatcher.addEventListener(EventName.NewMailCome, this.onNewMailComeHandler);
            Dispatcher.addEventListener(EventName.MailSendToPlayer, this.onMailSendToPlayerHandler);
            Dispatcher.addEventListener(EventName.MailGetAllAttachment, this.onMailGetAllAttachmentHandler);
            Dispatcher.addEventListener(EventName.MailGetAllAttachmentSuccess, this.onMailGetAllAttachmentSuccessHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._mailModule) 
            {
                this._mailModule = new MailModule();
                this._mailModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            if (this._mailModule.getSendPanelVisible()) 
            {
                cache.mail.isWriting = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Mail_StatusChange));
            }
            return this._mailModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            if (MailController._isFirstQuery) 
            {
                this._mailModule.setDefault();
                MailController._isFirstQuery = false;
            }
            else if (MailController._isHaveNew) 
            {
                this._mailModule.setDefault();
            }
            if (MailController._isHaveNew) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewMailHadQuery));
            }
            MailController._isHaveNew = false;
            return;
        }

        protected /* override */ initServer(): void
        {
            return;
        }

        /* internal  */getMailAttachment(arg1: DataEvent): void
        {
            var loc1=arg1.data["mailId"] as number;
            var loc2=arg1.data["index"] as int;
            var loc3=SystemSetter.localSetter.isDelMailAfterGet;
            GameProxy.mailProxy.getMailAttachment(loc1, loc2, loc3);
            return;
        }

        /* internal  */queryMail(arg1: DataEvent): void
        {
            var loc1=arg1.data["condition"];
            var loc2=arg1.data["type"];
            var loc3=arg1.data["status"];
            var loc4=arg1.data["startIndex"];
            var loc5=!SystemSetter.localSetter.isFilterBlacklist;
            GameProxy.mailProxy.queryMail(loc1, loc2, loc3, loc4, loc5);
            return;
        }

        /* internal  */readMail(arg1: DataEvent): void
        {
            var loc1=arg1.data as number;
            GameProxy.mailProxy.readMail(loc1);
            return;
        }

        /* internal  */removeMail(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.mailProxy.removeMail(loc1);
            return;
        }

        /* internal  */sendMail(arg1: DataEvent): void
        {
            var loc1=arg1.data["smail"] as SMail;
            var loc2=arg1.data["uids"] as Map<any, any> /* flash.utils.Dictionary */;
            var loc3=loc1.toPlayerName;
            var loc4=loc1.title;
            var loc5=loc1.content;
            var loc6=loc1.attachmentCoin;
            var loc7=loc1.attachmentGold;
            GameProxy.mailProxy.sendMail(loc3, loc4, loc5, loc6, loc7, loc2);
            return;
        }

        /* internal  */onMailQuerySuccess(arg1: DataEvent): void
        {
            this._mailModule.updateMailListProvider();
            return;
        }

        public onNewMailComeHandler(arg1: DataEvent): void
        {
            MailController._isHaveNew = true;
            if (isViewShow) 
            {
                this._mailModule.setDefault();
            }
            return;
        }

        public onMailSendToPlayerHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameController.mail.view.show();
            this._mailModule.writeMailToPlayer(loc1);
            return;
        }

        public onMailGetAllAttachmentHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            GameProxy.mailProxy.getMailAttachmentBatch(loc1);
            return;
        }

        public onMailGetAllAttachmentSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            MsgManager.showRollTipsMsg(Language.getString(52068));
            if (this._mailModule) 
            {
                this._mailModule.clearCollectAttachment();
                this._mailModule.setDefault();
            }
            return;
        }

        private /* var */_mailModule: IMailModule;

        private static /* var */_isHaveNew: Boolean=false;

        private static /* var */_isFirstQuery: Boolean=true;
    }

            MailController._isHaveNew = false;
            MailController._isFirstQuery = true;
        